<template>
  <view>
    <el-form :model="queryParams" ref="queryRef" size="normal" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="公告类型" clearable>
          <el-option v-for="dict in notice_type_dict" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="emit('confirm')">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd" v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          size="mini"
          :disabled="ids.length > 1"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          size="mini"
          :disabled="ids.length == 0"
          @click="handleDelete"
          v-hasPermi="['system:notice:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="emit('confirm')"></right-toolbar>
    </el-row>
  </view>
</template>

<script setup>
import { inject, watch } from "vue";
import { notice_type_dict } from "@/constant/dict";
const showSearch = ref(true);
const emit = defineEmits(["update:query", "confirm"]);
const $ = inject("$");
const queryOne = inject("queryOne");
const deleteOne = inject("deleteOne");
const { proxy } = getCurrentInstance();
const props = defineProps({
  query: {
    type: Object,
  },
  ids: {
    type: Array,
  },
});
// 通过定义计算属性
const queryParams = computed({
  get() {
    return props.query;
  },
  set(v) {
    emit("update:query", v);
  },
});
// 执行新建
function handleAdd() {
  $.value.doCreate();
}
// 执行更新
function handleUpdate() {
  let id = props.ids[0];
  queryOne(id).then((res) => {
    $.value.doUpdate(res);
  });
}
// 执行删除
function handleDelete() {
  let ids = props.ids;
  proxy.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项?').then(function () {
    let task = [];
    ids.forEach(function (ele, index) {
      task.push(deleteOne(ele));
    });
    Promise.all(task).then((res) => {
      emit('confirm')
      proxy.$modal.msgSuccess("删除成功");
    });
  });
}
function resetQuery() {
  proxy.resetForm("queryRef");
  emit("confirm");
}
</script>
